package com.yanyi.music.controller;

import com.alibaba.fastjson.JSONObject;
import com.yanyi.music.domain.Collect;
import com.yanyi.music.service.impl.CollectServiceImpl;
import com.yanyi.music.utils.Consts;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 收藏控制类
 */
@Api(tags = "收藏API")
@RestController
@RequestMapping("/collect")
public class CollectController {
    @Autowired
    private CollectServiceImpl collectService;

    /**
     * 添加收藏
     */
    @ApiOperation(value = "添加收藏")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Object addCollect(HttpServletRequest request) {
        JSONObject jsonObject = new JSONObject();
        //用户id
        String userId = request.getParameter("userId");
        //收藏类型（0歌曲1歌单）
        String type = request.getParameter("type");
        //歌曲id
        String songId = request.getParameter("songId");
        if (songId == null || songId.equals("")) {
            jsonObject.put(Consts.CODE, 0);
            jsonObject.put(Consts.MSG, "收藏歌曲为空");
            return jsonObject;
        }
        if (collectService.existSongId(Integer.parseInt(userId), Integer.parseInt(songId)) == 1) {
            jsonObject.put(Consts.CODE, 2);
            jsonObject.put(Consts.MSG, "已收藏");
            return jsonObject;
        }
        //保存到收藏的对象中
        Collect collect = new Collect();
        collect.setUserId(Integer.parseInt(userId));
        collect.setType(new Byte(type));
        collect.setSongId(Integer.parseInt(songId));
        collect.setCreateTime(new Date());
        int flag = collectService.insert(collect);
        if (flag == 1) {
            jsonObject.put(Consts.CODE, 1);
            jsonObject.put(Consts.MSG, "收藏成功");
            return jsonObject;
        }
        jsonObject.put(Consts.CODE, 0);
        jsonObject.put(Consts.MSG, "收藏失败");
        return jsonObject;
    }

    /**
     * 删除收藏
     */
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public Object deleteCollect(HttpServletRequest request) {
        //用户id
        String userId = request.getParameter("userId");
        //歌曲id
        String songId = request.getParameter("songId");
        int flag = collectService.deleteByUserIdSongId(Integer.parseInt(userId), Integer.parseInt(songId));
        return flag > 0 ? true : false;
    }

    /**
     * 查询所有收藏
     */
    @RequestMapping(value = "/allCollect", method = RequestMethod.GET)
    public Object allCollect(HttpServletRequest request) {
        return collectService.findAll(null);
    }

    /**
     * 查询某个用户的收藏列表
     */
    @RequestMapping(value = "/collectOfUserId", method = RequestMethod.GET)
    public Object collectOfUserId(HttpServletRequest request) {
        //用户id
        String userId = request.getParameter("userId");
        Map<String, Object> map = new HashMap<>();
        map.put("userId", userId);
        return collectService.findAll(map);
    }

}
